﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类ContractCommisionIncomeHistory。
    /// </summary>
    public class ContractCommisionIncomeHistory
    {
        public ContractCommisionIncomeHistory()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Contract_CommisionIncomeHistory(");
            strSql.Append("RecordId,ContractId,IncomeType,Income,AddTime,AddUserId,AddUserName,AddDepartId,AddDepartName)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@ContractId,@IncomeType,@Income,@AddTime,@AddUserId,@AddUserName,@AddDepartId,@AddDepartName)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "IncomeType", DbType.AnsiString, model.IncomeType);
            db.AddInParameter(dbCommand, "Income", DbType.Decimal, model.Income);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddUserId", DbType.Guid, model.AddUserId);
            db.AddInParameter(dbCommand, "AddUserName", DbType.AnsiString, model.AddUserName);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepartName", DbType.AnsiString, model.AddDepartName);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Contract_CommisionIncomeHistory set ");
            strSql.Append("ContractId=@ContractId,");
            strSql.Append("IncomeType=@IncomeType,");
            strSql.Append("Income=@Income,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("AddUserId=@AddUserId,");
            strSql.Append("AddUserName=@AddUserName,");
            strSql.Append("AddDepartId=@AddDepartId,");
            strSql.Append("AddDepartName=@AddDepartName");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "IncomeType", DbType.AnsiString, model.IncomeType);
            db.AddInParameter(dbCommand, "Income", DbType.Decimal, model.Income);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddUserId", DbType.Guid, model.AddUserId);
            db.AddInParameter(dbCommand, "AddUserName", DbType.AnsiString, model.AddUserName);
            db.AddInParameter(dbCommand, "AddDepartId", DbType.Guid, model.AddDepartId);
            db.AddInParameter(dbCommand, "AddDepartName", DbType.AnsiString, model.AddDepartName);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_CommisionIncomeHistory ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,IncomeType,Income,AddTime,AddUserId,AddUserName,AddDepartId,AddDepartName from t_Contract_CommisionIncomeHistory ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,IncomeType,Income,AddTime,AddUserId,AddUserName,AddDepartId,AddDepartName ");
            strSql.Append(" FROM t_Contract_CommisionIncomeHistory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,IncomeType,Income,AddTime,AddUserId,AddUserName,AddDepartId,AddDepartName ");
            strSql.Append(" FROM t_Contract_CommisionIncomeHistory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory> list = new List<Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory model = new Chenxin.Lib.Business.Entity.ContractCommisionIncomeHistory();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ContractId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ContractId = new Guid(ojb.ToString());
            }
            model.IncomeType = dataReader["IncomeType"].ToString();
            ojb = dataReader["Income"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Income = (decimal)ojb;
            }
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            ojb = dataReader["AddUserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddUserId = new Guid(ojb.ToString());
            }
            model.AddUserName = dataReader["AddUserName"].ToString();
            ojb = dataReader["AddDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddDepartId = new Guid(ojb.ToString());
            }
            model.AddDepartName = dataReader["AddDepartName"].ToString();
            return model;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetPartList(int PageSize, int PageIndex, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }
        #endregion  成员方法
    }
}

